package cn.edu.scau.cmi.chenjiale.singleton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * 主程序类
 * 
 * @author chen
 *
 */
public class ClientApplication extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		BorderPane pane = new BorderPane();
		HBox hBox = new HBox(5);
		Button singleTon = new Button("singleTon");
		Button multiton = new Button("multiton");
		hBox.getChildren().addAll(singleTon, multiton);
		TextArea textArea = new TextArea();
		pane.setCenter(textArea);
		pane.setTop(hBox);
		primaryStage.setScene(new Scene(pane, 200, 200));
		primaryStage.setTitle("创建单例对象");
		primaryStage.show();

		// 创建单例对象
		singleTon.setOnAction(e -> {
			Chairman chairman = Chairman.getInstance("毛主席");
			textArea.appendText("主席：" + chairman.getName() + "\n地址：" + chairman.toString() + "\n");
		});

		// 创建多例对象
		multiton.setOnAction(e -> {
			textArea.clear();
			Multipleton first = Multipleton.getRandomInstance();
			Multipleton second = Multipleton.getRandomInstance();
			textArea.setText("第1个对象编号：" + first.getNo() + "\n第2个对象编号：" + second.getNo() + "\n");
			textArea.appendText("是否是同一个对象：" + (first == second) + "\n");
		});

	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}
